package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.apis.wemedia.IWemediaClient;
import com.heima.model.admin.dtos.AuthDto;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.user.pojos.ApUserRealname;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.user.mapper.ApUserRealnameMapper;
import com.heima.user.service.ApUserRealnameService;
import com.heima.user.service.ApUserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ApUserRealnameServiceImpl extends ServiceImpl<ApUserRealnameMapper, ApUserRealname> implements ApUserRealnameService {
    @Override
    public PageResponseResult loadList(AuthDto dto) {
        if (dto.getPage() == null || dto.getSize() == null) {
            return (PageResponseResult) ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        LambdaQueryWrapper<ApUserRealname> queryWrapper = new LambdaQueryWrapper<>();
        if (dto.getStatus() != null) {
            queryWrapper.eq(ApUserRealname::getStatus, dto.getStatus());
        }
        IPage page = new Page(dto.getPage(), dto.getSize());
        this.page(page, queryWrapper);
        //封装结果
        PageResponseResult pageResponseResult = new PageResponseResult(dto.getPage(), (int) page.getSize(), (int) page.getTotal());
        pageResponseResult.setData(page.getRecords());
        return pageResponseResult;
    }

    @Override
    public ResponseResult authFail(AuthDto dto) {
        if (dto.getId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        ApUserRealname apUserRealname = this.getById(dto.getId());
        if (StringUtils.isNotBlank(dto.getMsg())) {
            apUserRealname.setReason(dto.getMsg());
        }
        apUserRealname.setStatus(ApUserRealname.Status.AUDIT_FAILED.getCode());
        this.updateById(apUserRealname);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Autowired
    private IWemediaClient iWemediaClient;

    @Autowired
    private ApUserService apUserService;

    @Override
    public ResponseResult authPass(AuthDto dto) {
        if (dto.getId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        ApUserRealname apUserRealname = this.getById(dto.getId());
        apUserRealname.setStatus(ApUserRealname.Status.AUDIT_PASS.getCode());
        this.updateById(apUserRealname);
        //存入wmUser
        ApUser apUser = apUserService.getById(apUserRealname.getUserId());
        WmUser wmUser = new WmUser();
        wmUser.setApUserId(apUserRealname.getUserId());
        apUser.setId(null);
        apUser.setCreatedTime(null);
        BeanUtils.copyProperties(apUser,wmUser);
        wmUser.setCreatedTime(new Date());
        iWemediaClient.saveAuth(wmUser);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
